package com.ingressalpha.android;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.TimePicker;

public class SettingsViewActivity extends Activity {

	private TextView mTimeDisplayStart;
	private Button mPickTimeStart;
	private TextView mTimeDisplayEnd;
	private Button mPickTimeEnd;

	private int mHourStart;
	private int mMinuteStart;

	private int mHourEnd;
	private int mMinuteEnd;

	static final int TIME_DIALOG_ID_1 = 0;
	static final int TIME_DIALOG_ID_2 = 1;

	private static String TAG = "settings-timer";

	TableLayout table;

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            If the activity is being re-initialized after previously being
	 *            shut down then this Bundle contains the data it most recently
	 *            supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it
	 *            is null.</b>
	 * 
	 * @author melkotek
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	}

	/**
	 * An OnClickListener that does the time setting.
	 * 
	 * @param color
	 * @return
	 */
	public void renderNewTimerForSetting(View view) {
		
		setContentView(R.layout.timeconfig);
		Spinner settingSpinner = (Spinner)findViewById(R.id.settingspinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.supported_settings,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		settingSpinner.setAdapter(adapter);
		settingSpinner.setOnItemSelectedListener(new SpinnerOnItemSelectedListener());
		
		mPickTimeStart = (Button) findViewById(R.id.buttonstart);
		mPickTimeEnd = (Button) findViewById(R.id.buttonstop);
		mTimeDisplayStart = (TextView) findViewById(R.id.timestart);
		mTimeDisplayEnd = (TextView) findViewById(R.id.timeend);

		// add a click listener to the button start
		mPickTimeStart.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID_1);
			}
		});

		// add a click listener to the button end
		mPickTimeEnd.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID_2);
			}
		});

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG_ID_1:
			return new TimePickerDialog(this, mTimeSetListenerStart,
					mHourStart, mMinuteStart, false);
		case TIME_DIALOG_ID_2:
			return new TimePickerDialog(this, mTimeSetListenerEnd, mHourEnd,
					mMinuteEnd, false);
		}
		return null;
	}

	private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHourStart = hourOfDay;
			mMinuteStart = minute;
			updateDisplay(mTimeDisplayStart, mHourStart, mMinuteStart);
		}
	};

	private TimePickerDialog.OnTimeSetListener mTimeSetListenerEnd = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHourEnd = hourOfDay;
			mMinuteEnd = minute;
			updateDisplay(mTimeDisplayEnd, mHourEnd, mMinuteEnd);
		}
	};

	// updates the time we display in the TextView
	private void updateDisplay(TextView timeView, int hour, int minute) {
		timeView.setText(new StringBuilder().append(pad(hour)).append(":")
				.append(pad(minute)));
	}

	private static String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}
	
	
	public class SpinnerOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	    	
	    	Spinner settingSpinner = (Spinner)findViewById(R.id.settingspinner);
			String text = settingSpinner.getSelectedItem().toString();
			
			// Check what is the selected setting type and append it
			// to the Header
			TextView header = (TextView)findViewById(R.id.textViewHeader);
			header.setText("New timer - " + text);	
	    	
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
	

}


